import React, { Component, createContext } from "react";
const { Provider, Consumer: CartConsumer } = createContext();

// import { b as c } from "a"
// const { b: c } = a;

// 这个组件要将购物车的整个功能封装起来
// CartProvider是将Provider做了一个二次封装
class CartProvider extends Component {
  state = {
    count: 6,
  };

  increment = () => {
    this.setState((prevState) => {
      return {
        count: prevState.count + 1,
      };
    });
  };

  decrement = () => {
    this.setState((prevState) => {
      return {
        count: prevState.count - 1,
      };
    });
  };
  render() {
    return (
      <Provider
        value={{
          count: this.state.count,
          increment: this.increment,
          decrement: this.decrement,
        }}
      >
        {this.props.children}
      </Provider>
    );
  }
}

export { CartProvider, CartConsumer };
